layui.use(['upload','form',"element"], function(){
    var upload = layui.upload;
    var form = layui.form;
    var element = layui.element;
    form.on('submit(*)', function(data){

        console.log(data.field);
        var value = data.field;
        if(data.field.curriculumUrl != "" &&  data.field.curriculumPictureurl != ""){
            $.ajax({
                url : "/curriculum/intermediate/insert",
                dataType : 'json',
                type : 'post',
                data :data.field,
                success : function(res) {
                    parent.refresh();
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                },
                errorfun : function() {

                }
            });
        }else {
            alert("请上传视频或图片")
        }


    });
    //执行实例
    var uploadInst = upload.render({
        elem: '#test1' //绑定元素
        ,url: '/YltUpload/' //上传接口
        ,done: function(res){
            console.log(res)
            var imgg = "http://43.143.176.39/"+res.data.fullPath;
            // $("img").src(imgg);
            $("input[name=curriculumPictureurl]").val(imgg)
            $("img").each(function(){

                $(this).attr('src',imgg);
            });
        }
        ,error: function(){
            //请求异常回调
        }
    });
    var uploadInst = upload.render({
        elem: '#test2' //绑定元素
        ,accept: 'video'
        ,url: '/curriculum/intermediate/upload/' //上传接口
        ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.open({
                type: 0,
                title: false,
                closeBtn: 0,
                btn: false,
                content: '<div class="layui-progress layui-progress-big" lay-filter="progress" lay-showpercent="true"><div class="layui-progress-bar"></div></div>'
                ,success:function(layero,index){
                    var scanTime = 1000;
                    var timer = setInterval(function (){
                        $.ajax({
                            url: "/curriculum/intermediate/upload/setInter",
                            success: function (data) {
                                var arr = data.msg

                                var percent = arr;
                                element.progress('progress', percent +'%')
                                if(percent  == 100){
                                    $.ajax({
                                        url : "/curriculum/intermediate/upload/deleteVideo",
                                        type : 'post',
                                        success : function(res) {
                                            clearInterval(timer)
                                        },
                                        errorfun : function() {

                                        }
                                    });
                                    //关闭弹出层
                                    var index = parent.layer.getFrameIndex(window.name);
                                    layer.close(index);
                                    layer.msg('保存成功')
                                }

                            },
                            error: function (e) {
                                //关闭定时器
                                clearInterval(timer);
                                //关闭弹出层
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                layer.msg("保存失败");

                            }
                        });
                    }, scanTime);
                }
             });

        }
        ,done: function(res){
            console.log(res)
            var imgg = res.path;
            $("input[name=curriculumUrl]").val(imgg);
            $("input[name=videoDuration]").val(res.h);
            $("video").attr("src",imgg);

        }
        ,error: function(){
            //请求异常回调
        }

    });

});